import request from '@/utils/request';
import type { 
  OrderReconciliationBatchQuery,
  OrderReconciliationQuery,
  OrderReconciliationBatchVO,
  OrderReconciliationVO,
  OrderReconciliationResultVO,
  ChargingOrderQueryVO,
  ChargingOrder,
  ChargingOrderStatisticVO,
  PageResult,
  OrderVerifyBatchQuery,
  OrderVerifyBatch,
  OrderVerifyRequestVO,
  Platform,
  OrderVerifyRecord
} from './types';
import type { IPage } from '@/types/global';

const BASE_URL = '/api/order';

// 导入Excel进行订单对账
export function importAndReconcile(data: FormData) {
  return request.post<Map<string, any>>(`${BASE_URL}/import`, data);
}

// 直接提交订单数据进行对账
export function submitAndReconcile(data: OrderReconciliationResultVO[]) {
  return request.post<Map<string, any>>(`${BASE_URL}/submit`, data);
}

// 保存对账结果
export function saveReconciliationResult(batchName: string, resultList: OrderReconciliationResultVO[]) {
  return request.post<string>(`${BASE_URL}/save`, resultList, {
    params: { batchName }
  });
}

// 导出对账结果Excel
export function exportReconciliationResult(data: OrderReconciliationResultVO[]) {
  return request.post(`${BASE_URL}/export`, data, {
    responseType: 'blob'
  });
}

// 分页查询对账批次
export function getBatchPage(params: OrderReconciliationBatchQuery) {
  return request.post<IPage<OrderReconciliationBatchVO>>(`${BASE_URL}/batch/page`, params);
}

// 获取对账批次详情
export function getBatchDetail(id: number) {
  return request.get<OrderReconciliationBatchVO>(`${BASE_URL}/batch/${id}`);
}

// 根据批次号获取对账批次详情
export function getBatchDetailByBatchNo(batchNo: string) {
  return request.get<OrderReconciliationBatchVO>(`${BASE_URL}/batch/no/${batchNo}`);
}

// 分页查询对账记录
export function getRecordPage(params: OrderReconciliationQuery) {
  return request.post<IPage<OrderReconciliationVO>>(`${BASE_URL}/record/page`, params);
}

// 获取对账记录详情
export function getRecordDetail(id: number) {
  return request.get<OrderReconciliationVO>(`${BASE_URL}/record/${id}`);
}

// 根据批次号查询对账记录列表
export function getRecordsByBatchNo(batchNo: string) {
  return request.get<OrderReconciliationVO[]>(`${BASE_URL}/record/batch/${batchNo}`);
}

// 导出对账记录Excel
export function exportRecords(params: OrderReconciliationQuery) {
  return request.post(`${BASE_URL}/record/export`, params, {
    responseType: 'blob'
  });
}

// 根据批次号获取核对记录
export function getVerifyRecords(batchNo: string) {
  return request.get<OrderVerifyRecord[]>(`${BASE_URL}/verify/records/${batchNo}`);
}

// 根据批次号获取核对批次详情
export function getVerifyBatch(batchNo: string) {
  return request.get<OrderVerifyBatch>(`${BASE_URL}/verify/batch/${batchNo}`);
}

// 获取订单列表
export function getOrderList(params: ChargingOrderQueryVO) {
  return request.get<PageResult<ChargingOrder>>(`${BASE_URL}/list`, { params });
}

// 导出订单数据
export function exportOrders(params: ChargingOrderQueryVO) {
  return request.get(`${BASE_URL}/export`, { 
    params,
    responseType: 'blob'
  });
}

// 同步订单
export function syncOrder(orderNo: string) {
  return request.post(`${BASE_URL}/sync/${orderNo}`);
}

// 批量同步订单
export function batchSyncOrders(orderNos: string[]) {
  return request.post(`${BASE_URL}/sync/batch`, { orderNos });
}

// 获取订单统计数据
export function getOrderStatistics(params: ChargingOrderQueryVO) {
  return request.get<ChargingOrderStatisticVO>(`${BASE_URL}/statistic`, { params });
}

export function getVerifyBatchList(params: OrderVerifyBatchQuery) {
  return request.get<PageResult<OrderVerifyBatch>>('/api/order/verify/batch/list', {
    params,
  });
}

export function verifyOrders(platformId: number, verifyRequests: OrderVerifyRequestVO[]) {
  return request.post<string>('/api/order/verify/batch', verifyRequests, {
    params: { platformId }
  });
}

// 获取平台列表
export function getPlatformList() {
  return request.get<Platform[]>('/api/platform/list');
} 